package cn.ldw.service.impl;

import cn.ldw.mapper.RoleMapper;
import cn.ldw.mapper.SignInfoMapper;
import cn.ldw.mapper.UserMapper;
import cn.ldw.pojo.Role;
import cn.ldw.pojo.Signinfo;
import cn.ldw.pojo.User;
import cn.ldw.service.UserService;
import cn.ldw.utils.ErrorInfo;
import cn.ldw.utils.JwtTokenUtils;
import cn.ldw.utils.UserThreadLocal;
import cn.ldw.vo.*;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {

    @Autowired(required = false)
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired(required = false)
    private SignInfoMapper signInfoMapper;

    @Autowired(required = false)
    private RoleMapper roleMapper;

    @Override
    public Result login(LoginParams loginParams) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, loginParams.getUsername());
        User user = userMapper.selectOne(queryWrapper);
        if (user == null) {
            return Result.fail(ErrorInfo.USER_EXIST.getCode(), ErrorInfo.USER_EXIST.getMessage());
        }
        queryWrapper.eq(User::getPassword, loginParams.getPassword());
        user = userMapper.selectOne(queryWrapper);
        if (user == null) {
            return Result.fail(ErrorInfo.USERINFO_ERROR.getCode(), ErrorInfo.USERINFO_ERROR.getMessage());
        }
        String token = JwtTokenUtils.createToken(user.getId());
        redisTemplate.opsForValue().set("token_" + token, JSON.toJSONString(user), 1000, TimeUnit.DAYS);
        return Result.success(token);
    }

    @Override
    public Result register(LoginParams loginParams) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, loginParams.getUsername());
        User user = userMapper.selectOne(queryWrapper);
        if (user != null) {
            return Result.fail(ErrorInfo.USERNAME_EXIST.getCode(), ErrorInfo.USERNAME_EXIST.getMessage());
        }
        User newUser = new User();
        newUser.setUsername(loginParams.getUsername());
        newUser.setPassword(loginParams.getPassword());
        newUser.setCreationDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        newUser.setUserRole(2);
        userMapper.insert(newUser);
        return Result.success(null);
    }

    @Override
    public Result loginout(String token) {
        redisTemplate.delete("token_" + token);
        return Result.success(null);
    }

    @Override
    public Result info() {
        UserInfoVo userInfoVo = new UserInfoVo();
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        User user = UserThreadLocal.get();
        queryWrapper.select(Role::getRouters);
        queryWrapper.eq(Role::getId, user.getUserRole());
        Role role = roleMapper.selectOne(queryWrapper);
        userInfoVo.setUser(user);
        userInfoVo.setRouters(role.getRouters().split(","));
        return Result.success(userInfoVo);
    }

    @Override
    public Result modify(ModifyParams modifyParams) {
        User user = UserThreadLocal.get();
        user.setPassword(modifyParams.getNewpassword());
        userMapper.updateById(user);
        return Result.success(null);
    }

    @Override
    public Result userInfo() {
        List<User> userList = userMapper.selectList(null);
        return Result.success(userList);
    }

    @Override
    public Result addUser(UserParams userParams) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, userParams.getUsername());
        User user = userMapper.selectOne(queryWrapper);
        if (user != null) {
            return Result.fail(ErrorInfo.USERNAME_EXIST.getCode(), ErrorInfo.USERNAME_EXIST.getMessage());
        }
        User newUser = new User();
        newUser.setUserRole(userParams.getRole());
        newUser.setUsername(userParams.getUsername());
        newUser.setPassword(userParams.getPassword());
        newUser.setCreationDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        userMapper.insert(newUser);
        return Result.success(null);
    }

    @Override
    public Result delete(User user) {
        userMapper.deleteById(user.getId());
        LambdaQueryWrapper<Signinfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Signinfo::getUsername, user.getUsername());
        signInfoMapper.delete(queryWrapper);
        return Result.success(null);
    }

    @Override
    public Result modifyUser(UserParams userParams) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, userParams.getUsername());
        User user = new User();
        user.setPassword(userParams.getPassword());
        user.setUserRole(userParams.getRole());
        userMapper.update(user, queryWrapper);
        return Result.success(null);
    }
}
